package com.ubireality.service.resource;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;

import org.apache.log4j.Logger;

import com.ubireality.module.player.User;
import com.ubireality.repository.UserRepository;

@Path("/")
public class UserResource {
	@Inject
	private Logger log;
	@Inject
	private UserRepository userRepository;

	@PostConstruct
	public void init() {
		log.info("API in");

	}

	@GET
	@Path("id/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRes(@PathParam("id") long id) {
		User user= userRepository.findById(id);
		ResponseBuilder bulider = Response.ok(user);
		return bulider.build();
	}

	@GET
	@Path("user")
	@Produces(MediaType.APPLICATION_JSON)
	public User getUserDetail() {
		User u = new User();
		u.setDeviceId("test id");
		u.setName("the first user");
		return u;
	}

	@GET
	@Path("user/all")
	@Produces(MediaType.APPLICATION_JSON)
	public List<User> getUserAll() {
		return userRepository.all();
	}

	@GET
	@Path("test")
	@Produces("text/plain")
	public StreamingOutput get() {
		log.info("GET API called");
		return new StreamingOutput() {
			public void write(OutputStream output) throws IOException,
					WebApplicationException {
				output.write("hello world".getBytes());
			}
		};
	}
}
